//
//  SegmentBaseViewController.m
//  Cat
//
//  Created by lushitong on 2019/1/3.
//  Copyright © 2019年 lushitong. All rights reserved.
//

#import "SegmentBaseViewController.h"
#import "HomeViewController.h"
#import "ListViewController.h"
#import "HomeRecordViewController.h"

@interface SegmentBaseViewController () <JXCategoryViewDelegate, UIScrollViewDelegate>

@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, strong) NSMutableArray *listArray;

@end

@implementation SegmentBaseViewController

- (instancetype)init
{
	self = [super init];
	if (self) {
		_shouldHandleScreenEdgeGesture = YES;
	}
	return self;
}

- (void)viewDidLoad {
	[super viewDidLoad];
	self.edgesForExtendedLayout = UIRectEdgeNone;
	
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
	CGFloat index = scrollView.contentOffset.x/scrollView.bounds.size.width;
	CGFloat absIndex = fabs(index - self.currentIndex);
	if (absIndex >= 2) {
		//嵌套使用的时候，最外层的VC持有的scrollView在翻页之后，就断掉一次手势。解决快速滑动的时候，只响应最外层VC持有的scrollView。子VC持有的scrollView却没有响应
		self.scrollView.panGestureRecognizer.enabled = NO;
		self.scrollView.panGestureRecognizer.enabled = YES;
		_currentIndex = floor(index);
		ListViewController *listVC = [self.listArray safeObjectAtIndex:absIndex - 2];
		[listVC sendRequestWithID:[self.array safeObjectAtIndex:absIndex - 2]];
	}
}

- (void)requestAndLoadAllViews {
	CGFloat naviHeight = 64;
	if (@available(iOS 11.0, *)) {
		if (ScreenHeight == 812) {
			naviHeight = [UIApplication sharedApplication].keyWindow.safeAreaInsets.top + 44;
		}
	}
	[self.listArray removeAllObjects];
	
	NSUInteger count = [self preferredListViewCount];
	CGFloat categoryViewHeight = [self preferredCategoryViewHeight];
	CGFloat width = ScreenWidth;
	CGFloat height = ScreenHeight - NavigationBarHeight - StatusBarHeight - kQKFitModuleDecimal(32.0) + kQKFitModuleDecimal(3.0);
	
	_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.topTitleView.bottom, width, height)];
	self.scrollView.delegate = self;
	self.scrollView.pagingEnabled = YES;
	self.scrollView.contentSize = CGSizeMake(width*count, height);
	self.scrollView.bounces = NO;
	[self.view addSubview:self.scrollView];
	
	if (@available(iOS 11.0, *)) {
		self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
	}
	
	for (int i = 0; i < count; i ++) {
		if (i == 0) {
			HomeRecordViewController *listVC = [[HomeRecordViewController alloc] init];
			[self configListViewController:listVC index:i];
			[self addChildViewController:listVC];
			listVC.view.frame = CGRectMake(i*width, categoryViewHeight - kQKFitModuleDecimal(1), width, height);
			[self.scrollView addSubview:listVC.view];
		} else {
			ListViewController *listVC = [[[self preferredListViewControllerClass] alloc] init];
			[self.listArray addObject:listVC];
			[self configListViewController:listVC index:i];
			[self addChildViewController:listVC];
			listVC.view.frame = CGRectMake(i*width, categoryViewHeight - kQKFitModuleDecimal(1), width, height);
			[self.scrollView addSubview:listVC.view];
		}
	}
	
	self.categoryView.frame = CGRectMake(0, self.topTitleView.bottom - kQKFitModuleDecimal(1.0), ScreenWidth, categoryViewHeight);
	self.categoryView.backgroundColor = COLOR_WITH_HEX(0xFFC236);
	self.categoryView.delegate = self;
	self.categoryView.contentScrollView = self.scrollView;
	[self.view addSubview:self.categoryView];
}

- (void)viewDidAppear:(BOOL)animated {
	[super viewDidAppear:animated];
	self.navigationController.interactivePopGestureRecognizer.enabled = (self.categoryView.selectedIndex == 0);
}

- (Class)preferredCategoryViewClass {
	return [JXCategoryBaseView class];
}

- (NSUInteger)preferredListViewCount {
	return 0;
}

- (CGFloat)preferredCategoryViewHeight {
	return kQKFitModuleDecimal(32);
}

- (Class)preferredListViewControllerClass {
	return [ListViewController class];
}

- (void)configListViewController:(UIViewController *)controller index:(NSUInteger)index {
	
}

- (JXCategoryBaseView *)categoryView {
	if (_categoryView == nil) {
		_categoryView = [[[self preferredCategoryViewClass] alloc] init];
	}
	return _categoryView;
}

- (void)rightItemClicked {
	JXCategoryIndicatorView *componentView = (JXCategoryIndicatorView *)self.categoryView;
	for (JXCategoryIndicatorComponentView *view in componentView.indicators) {
		if (view.componentPosition == JXCategoryComponentPosition_Top) {
			view.componentPosition = JXCategoryComponentPosition_Bottom;
		}else {
			view.componentPosition = JXCategoryComponentPosition_Top;
		}
	}
	[componentView reloadData];
}

#pragma mark - JXCategoryViewDelegate

- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
	//侧滑手势处理
	if (_shouldHandleScreenEdgeGesture) {
		self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
	}
	if (index > 2) {
		
	}
	
}

- (NSMutableArray *)array {
	if (!_array) {
		_array = [[NSMutableArray alloc] init];
	}
	return _array;
}

- (NSMutableArray *)listArray {
	if (!_listArray) {
		_listArray = [[NSMutableArray alloc] init];
	}
	return _listArray;
}

@end
